package com.chinasi.web.action.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.chinasi.model.Dict;
import com.chinasi.model.SysDict;
import com.chinasi.service.SysDictService;

/** 字典数据缓存 */
@Component
public class DictCache implements InitializingBean{
	
	@Autowired
	private SysDictService dictService;

	private static HashMap<String, List<Object>> cache = new HashMap<String, List<Object>>();
	
	public static <T> List<T> getByCode(String key, Class<T> beanClass){
		if(cache.get(key) != null){
			return (List<T>)cache.get(key);
		}
		return null;
	}
	
	public void put(String key, List list){
		cache.put(key, list);
	}
	
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<String>();
		list.add("北京");
		list.add("上海");
		list.add("西安");
		DictCache dictCache = new DictCache();
		dictCache.put(SystemConstants.DictCode.area.toString(), list);
		
		List<String> cacheResult = dictCache.getByCode(SystemConstants.DictCode.area.toString(), String.class);
		System.out.println(cacheResult);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		List<SysDict> dictList = dictService.findAll();
		for(SysDict dict : dictList){
			this.put(dict.getDictCode(), dict.getDictDataList());
		}
	}
}
